package com.mycat.task.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.mycat.common.entity.InvokeResult;
import com.mycat.task.dto.CommonDTO;
import com.mycat.task.entity.ScheduleTaskExecuteDetail;
import com.mycat.task.service.ScheduleTaskExecuteDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping("/scheduleTaskExecuteDetail")
public class ScheduleTaskExecuteDetailController {

	private static final Logger logger = LoggerFactory.getLogger(ScheduleTaskExecuteDetailController.class);

	@Autowired
	private ScheduleTaskExecuteDetailService scheduleTaskExecuteDetailService;

	/**
	 * 页面跳转
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String toList() {
		return "task/ScheduleTaskExecuteDetail";
	}

	/**
	 * 获取分页数据
	 *
	 * @param pageNum  页码
	 * @param pageSize 页大小
	 */
	@RequestMapping(value = "/{pageNum}/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<InvokeResult<PageInfo<ScheduleTaskExecuteDetail>>> page(@PathVariable int pageNum, @PathVariable int pageSize) {
		InvokeResult invokeResult = null;
		try {
			Page<ScheduleTaskExecuteDetail> page = new Page<ScheduleTaskExecuteDetail>() {{
				this.setPageNum(pageNum);
				this.setPageSize(pageSize);
			}};
			PageInfo<ScheduleTaskExecuteDetail> pageInfo = scheduleTaskExecuteDetailService.page(page);
			if (null != pageInfo && null != pageInfo.getList() && pageInfo.getList().size() > 0) {
				invokeResult = InvokeResult.success(pageInfo);
			} else {
				invokeResult = InvokeResult.pass_no_data();
			}
		} catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return new ResponseEntity<>(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
	}

	/**
	 * 获取单个对象
	 *
	 * @param id Id
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<InvokeResult<ScheduleTaskExecuteDetail>> getScheduleTaskExecuteDetail(@PathVariable Integer id) {
		InvokeResult invokeResult = null;
		try {
			if (null != id) {
				ScheduleTaskExecuteDetail scheduleTaskExecuteDetail = scheduleTaskExecuteDetailService.findScheduleTaskExecuteDetail(id);
				invokeResult = InvokeResult.success(scheduleTaskExecuteDetail);
			} else {
				invokeResult = InvokeResult.failure("参数错误！");
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return new ResponseEntity(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
    }

	/**
	 * 删除对象
	 *
	 * @param commonDTO 删除数据DTO
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResponseEntity<InvokeResult> delete(@RequestBody CommonDTO commonDTO) {
		InvokeResult invokeResult = null;
		try {
			if (null != commonDTO && null != commonDTO.getIds() && commonDTO.getIds().length > 0) {
				scheduleTaskExecuteDetailService.delete(commonDTO.getIds());
				invokeResult = InvokeResult.success();
			} else {
				invokeResult = InvokeResult.failure("参数错误！");
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return new ResponseEntity(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
    }

    /**
     * 新增数据
     *
     * @param scheduleTaskExecuteDetail 新增对象
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<InvokeResult> add(ScheduleTaskExecuteDetail scheduleTaskExecuteDetail) {
        InvokeResult invokeResult = null;
        try {
			if (null != scheduleTaskExecuteDetail) {
				scheduleTaskExecuteDetailService.save(scheduleTaskExecuteDetail);
				invokeResult = InvokeResult.success();
			} else {
				invokeResult = InvokeResult.failure("参数错误！");
			}
        } catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
        }
        return new ResponseEntity(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
    }

	/**
	 * 修改数据
	 *
	 * @param scheduleTaskExecuteDetail 修改对象
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<InvokeResult> edit(ScheduleTaskExecuteDetail scheduleTaskExecuteDetail) {
		InvokeResult invokeResult = null;
		try {
			if (null != scheduleTaskExecuteDetail && null != scheduleTaskExecuteDetail.getId()) {
				scheduleTaskExecuteDetailService.edit(scheduleTaskExecuteDetail);
				invokeResult = InvokeResult.success();
			} else {
				invokeResult = InvokeResult.failure("参数错误！");
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return new ResponseEntity(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
	}

}